'''
Created on Jul 9, 2010

@author: riche
'''
from ft.framework import io_component
import socket, re

class NetWriterComponent(io_component.IOComponent):
    '''
    classdocs
    '''

    def __init__(self, name, node_name, dir_instance):
        '''
        Constructor
        '''
        io_component.IOComponent.__init__(self, name)
        self.__outputs__ = []
        self.__directory__ = dir_instance
        self._node_name = node_name
        
        
    def handle_input_stream(self, msg):
        # get the host_name and data out of the msg tuple
        try:
            host_name = msg[0]
            data = msg[1]
        except Exception:
            print('The msg argument to NetWriterComponent is not a (host_name, data) tuple')
            exit(2)
            
        m = re.search('BCAST;(\S+)', msg[0])
        if m is None:
            self.send(host_name, data)
        else:
            for de in self.__directory__.get_group(m.group(1)):
                if de.short_name != self._node_name:
                    #print(self._node_name + ':' + de.short_name)
                    self.send(de.short_name, data)

    def send(self, host_name, data):
        send_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        address = self.__directory__.address(host_name)
        port = self.__directory__.listen_port(host_name)
        try:
            print('sending: {} to {}'.format(str(data),host_name))
            send_sock.connect((address, port))
            send_sock.sendall(data.encode('utf-8'))
        except socket.error as err:
            print('Error on connect to ' + str(address) + ':' + str(port))
            print(err)
        except socket.error as err:
            print('Error on send to ' + str(address) + ':' + (port))
            print(err)
            
        send_sock.close()     
    
    def cleanup(self):
        '''Cleanup any sockets that have been stored. Currently we don't store sockets'''
        for sock in self.__outputs__:
            sock.close()   